package com.powernode.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.reggie.common.CustomException;
import com.powernode.reggie.dto.SetmealDto;
import com.powernode.reggie.entity.Setmeal;
import com.powernode.reggie.entity.SetmealDish;
import com.powernode.reggie.mapper.SetmealMapper;
import com.powernode.reggie.service.SetmealDishService;
import com.powernode.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: www
 * @time: 2023/3/8 17:15
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 将传入的数据存在Setmeal和SetmealDish表中；
     * @param setmealDto
     */
    @Override
    @Transactional//加事务注解
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息，操作Setmeal 执行insert
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();//取出Setmealdish，其中Setmeal id 没值，只有dish id值，所以需要导入


        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());//将Setmeal id 赋值给Setmealdish上
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品关联信息， 操作SetmealDish 执行insert
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 删除套餐  同时删除关联的Setmeal_dish
     * @param ids
     */
    @Override
    @Transactional//删除两张表， 需要加入事务注解
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，判断是否能删除
        //构造查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        //不能删除，抛出一个业务异常
        if(count > 0){
            throw new CustomException("套餐正在售卖， 不能删除");
        }

        //停售状态 可以删除，先删除套餐数据 setmeal
        this.removeByIds(ids);


        //再删除关系表数据  setmeal_dish
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.in(SetmealDish::getSetmealId,ids);//构造删除条件，setmeal_id在ids里面

        setmealDishService.remove(queryWrapper1);
    }

    //查询套餐信息和菜品信息 根据套餐id
    @Override
    public SetmealDto getWithDish(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    //修改套餐信息和菜品信息
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);//修改Setmeal数据

        //先删除SetmealDish表对应数据
        Long setmeald = setmealDto.getId();//获取Setmeal套餐id
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeald);
        setmealDishService.remove(queryWrapper);//删除

        //然后再新增
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmeald);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}
